﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
//modified by jeff.yuan 增加通过URL传查询条件，传客户和日期
public partial class Message_Manage : PageBase
{
    private const string NEW_PAGE = "Customer_Select.aspx";
    private OrgainzationManager orgManager = new OrgainzationManager();
    MessageManager messagemanager = new MessageManager();
    protected void Page_Load(object sender, EventArgs e)
    {

        



        if (!IsPostBack)
        {
            DateTime dt = DateTime.Now;

            txtEnd.Text = dt.AddMonths(1).ToString("yyyy-MM-dd");
            txtStart.Text = dt.ToString("yyyy-MM-dd");
            
            DataBind();
            if (txtHidOrg.Value == "")
            {
                this.txtHidOrg.Value = OrgHidValue;
                
                //OrgHelper.BindChiansDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlChains);
                var data = new OrgainzationManager().SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, null);
                //if (data.Rows.Count != 1)
                //{
                //    OrgHelper.BindUsersDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlUser);
                //}
                //else
                //{
                //    Person person = new PersonManager().Load(MyStateManager.PersonID);
                //    ddlUser.Items.Add(new ListItem(person.Name, MyStateManager.UserID.ToString()));
                //}

            }
            InitForm();
            
               
     
             BindGrid();
           
            


        }
    }
   
    private void InitForm()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();
        //销售渠道
        ddlMessageLevel.DataTextField = "Name";
        ddlMessageLevel.DataValueField = "DictionaryItemId";
        ddlMessageLevel.DataSource = dim.GetItemList(DictionaryEnum.MessageType, this.MyStateManager.DomainID);
        ddlMessageLevel.DataBind();
        ddlMessageLevel.Items.Insert(0, new ListItem("-所有-", ""));
    }

    private void BindGrid()
    {

       
        System.Text.StringBuilder sb = new System.Text.StringBuilder(" ");

      
        if (ddlMessageLevel.SelectedValue != string.Empty)
        {
            sb.Append(" And  CONTENT_LEVEL = '" + ddlMessageLevel.SelectedValue + "' ");
        }
       
        if (txtCode.Text != string.Empty)
        {
            sb.Append(" AND (TOPIC LIKE '%" + txtCode.Text.Trim() + "%' or  CONTENT LIKE '%" + txtCode.Text.Trim() + "%')");
        }

        if (txtStart.Text != string.Empty)
        {
            //sb.Append(" AND DATEDIFF(DD,BEGIN_DATE,'" + DateTime.Parse(txtStart.Text).ToShortDateString() + "')<=0 ");
            sb.Append(" AND BEGIN_DATE>=" + txtStart.Text.Replace("-","").Replace("/",""));
        }
        if (txtEnd.Text != string.Empty)
        {
            //sb.Append(" AND DATEDIFF(DD,END_DATE,'" + DateTime.Parse(txtEnd.Text).ToShortDateString() + "')>=0 ");
            sb.Append(" AND END_DATE<=" + txtEnd.Text.Replace("-","").Replace("/",""));
        }
        if (ddlActive.SelectedValue != string.Empty)
        {
            sb.Append(" AND  ACTIVE='" + ddlActive.SelectedValue + "'  ");
        }
        
        MessageManager mm = new MessageManager();
        Jumpton.Persistent.ObjectTable dt = mm.GetSendMessageList(MyStateManager.DomainID, sb.ToString(), MyStateManager.Name);
        if (dt != null)
        {
            GridPager1.DataSource = dt;
            GridPager1.GridView = gvMessage;
            GridPager1.BindData();

            GridPager1.Visible = gvMessage.Rows.Count > 0;
           
        }

         



    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
        //SaveConditions();
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }

   

    protected  void   Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

           // messagemanager.UpdateReadCount(gvMessage.DataKeys[e.Row.RowIndex].Value.ToString());//更新已读消息数量
            int rowCount = gvMessage.Columns.Count;
            if (e.Row.Cells[rowCount - 1].Text.IndexOf("Y") > -1)
            {
                e.Row.Cells[rowCount - 1].Text = "无效";
            }
            else
            {
                if (e.Row.Cells[rowCount - 1].Text.IndexOf("N") > -1)
                {
                    e.Row.Cells[rowCount - 1].Text = "有效";
                }
            }
           
            e.Row.Cells[1].Text = new DictionaryItemsManager().Load(decimal.Parse(e.Row.Cells[1].Text)).Name;
            e.Row.Cells[2].Text = DateTime.ParseExact(e.Row.Cells[2].Text, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture
                      , System.Globalization.DateTimeStyles.None).ToString("yyyy-MM-dd");
            e.Row.Cells[3].Text = DateTime.ParseExact(e.Row.Cells[3].Text, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture
                    , System.Globalization.DateTimeStyles.None).ToString("yyyy-MM-dd");
        }
    }

    
   //protected void SaveConditions()
    //{
    //    string pagename = "order_list";
    //    Session[pagename + "ddluser"] = ddlUser.SelectedValue;
    //    Session[pagename + "ddlordertype"] = ddlOrderType.SelectedValue;
    //    Session[pagename + "ddlchains"] = ddlChains.SelectedValue;
    //    Session[pagename + "txtstart"] = txtStart.Text;
    //    Session[pagename + "txtend"] = txtEnd.Text;
    //    Session[pagename + "txtcode"] = txtCode.Text;
    //    Session[pagename + "ddlchannel"] = ddlChannel.SelectedValue;
    //    Session[pagename + "ddlstatus"] = ddlStatus.SelectedValue;
    //    Session[pagename + "txtorderno"] = txtOrderNo.Text;
    //    Session[pagename + "drpcancelled"] = drpCancelled.SelectedValue;
    //    Session[pagename + "isactive"] = "1";

    //}
    //protected void LoadConditions()
    //{
    //    string pagename = "order_list";
    //    if (Session[pagename + "isactive"] != null && Session[pagename + "isactive"].ToString() == "1")
    //    {
    //        try
    //        {
    //            ddlUser.SelectedValue = Session[pagename + "ddluser"].ToString();
    //            ddlOrderType.SelectedValue = Session[pagename + "ddlordertype"].ToString();
    //            ddlChains.SelectedValue = Session[pagename + "ddlchains"].ToString();
    //            txtStart.Text = Session[pagename + "txtstart"].ToString();
    //            txtEnd.Text = Session[pagename + "txtend"].ToString();
    //            txtCode.Text = Session[pagename + "txtcode"].ToString();
    //            ddlChannel.SelectedValue = Session[pagename + "ddlchannel"].ToString();
    //            ddlStatus.SelectedValue = Session[pagename + "ddlstatus"].ToString();
    //            txtOrderNo.Text = Session[pagename + "txtorderno"].ToString();
    //            drpCancelled.SelectedValue = Session[pagename + "drpcancelled"].ToString();
    //        }
    //        catch
    //        {
    //        }
    //        Session[pagename + "isactive"] = "0";
    //    }

    //}
    //protected void txtOrg_TextChanged(object sender, EventArgs e)
    //{
    //    ddlChains.Items.Clear();
    //    ddlUser.Items.Clear();

    //    if (!string.IsNullOrEmpty(txtHidChains.Value))
    //    {
    //        string[] options = txtHidChains.Value.Split('∮');
            

    //        for (int i = 0; i < options.Length; i++)
    //        {
    //            string[] item = options[i].Split('∞');
    //            ddlChains.Items.Add(new ListItem(item[1], item[0]));
    //        }
      
    //    }

    //    //判断当前的人所在的部门是不是最小的一级org
    //    var data = new OrgainzationManager().SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, null);
    //    if (data.Rows.Count != 1)
    //    {
    //        if (!string.IsNullOrEmpty(txtHidUsers.Value))
    //        {
    //            string[] options2 = txtHidUsers.Value.Split('∮');
    //            for (int i = 0; i < options2.Length; i++)
    //            {
    //                string[] item2 = options2[i].Split('∞');
    //                ddlUser.Items.Add(new ListItem(item2[1], item2[0]));
    //            }
    //        }
    //        ddlUser.Items.Insert(0, new ListItem("--所有--", ""));
    //    }
    //    else
    //    {
    //        Person person = new PersonManager().Load(MyStateManager.PersonID);
    //        ddlUser.Items.Add(new ListItem(person.Name,MyStateManager.UserID.ToString()));
    //    }
    //    ddlChains.Items.Insert(0, new ListItem("--所有--", ""));
        

    //}



    //protected void btnQAdd_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("~/Login.aspx");
    //}
   

    
}
